package com.cskaoyan.tool;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.mapper.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 *  : 工具类中如何使用 组件？？？ 已解决...
 * xdx
 * 2020/12/10
 * 10:22
 */
@Component
public class CouponTool {

    /**
     * 此变量不能使用static修饰。
     * 容器和类加载器的关系
     */
    @Autowired
    private CouponMapper couponMapper;

    private static CouponTool couponTool;

    /**
     * 验证优惠券是否超时
     * @param coupon
     * @return true : 超时
     */
    public static boolean couponTimeCheck(Coupon coupon) {
        Date updateTime = coupon.getUpdateTime();
        Short timeType = coupon.getTimeType();
        if (timeType == 0){
            Short days = coupon.getDays();
            Date nowDate = new Date();
            Date lastUpdateTime = updateTime == null ? coupon.getAddTime():updateTime;
            long time = nowDate.getTime() - lastUpdateTime.getTime();
            if (time >= (long) days * FinalVariable.millisecondOfDay){
                return true;
            }
        }else if (timeType == 1){
            Date nowDate = new Date();
            long time = nowDate.getTime() - coupon.getEndTime().getTime();
            if (time >= 0){
                return true;
            }
        }
        return false;
    }

    /**
     * 每次访问数据库的coupon表格，都要更新一次。如果数据库数据过多，时间会需要多少？需要测试
     * 包括 status : status每时每刻都在变化
     *      异常：couponMapper.selectByExample(couponExample);空指针，所以status没有更新
     *      原因：couponMapper = null
     */
    public static void updateDataSource()   {
//        CouponExample couponExample = new CouponExample();
//        CouponExample.Criteria criteria = couponExample.createCriteria();
//        criteria.andStatusBetween(Short.valueOf("-1"),Short.valueOf("3"));
        CouponMapper couponMapper = getCouponMapper();
        System.out.println("couponMapper = " + couponMapper);
        List<Coupon> coupons = couponMapper.selectAllCoupons();
        Iterator<Coupon> iterator = coupons.iterator();
        while (iterator.hasNext()){
            Coupon coupon = iterator.next();
            boolean b = couponTimeCheck(coupon);
            if (b){
                coupon.setStatus((short) 1);
                updateCoupon(coupon,couponMapper);
            }
            if (coupon.getDeleted() == true){
                coupon.setStatus((short) 2);
                updateCoupon(coupon,couponMapper);
            }

        }

    }





    private static void updateCoupon(Coupon coupon, CouponMapper couponMapper)   {
        int i = couponMapper.updateByPrimaryKeySelective(coupon);
        if (1 != i) {
//            throw new Exception("自动更新数据库失败");
            System.out.println(" 自动更新数据库失败");
        }
    }

    /**
     * 生命周期：？依赖注入完成后被自动调用。？？？？TODO
     */
    @PostConstruct
    public void init(){
        couponTool = this;
        couponTool.couponMapper = this.couponMapper;
    }

    private CouponTool(){

    }

    private static CouponMapper getCouponMapper(){
        return couponTool.couponMapper;
    }

}
